package com.mine.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mine.pojo.MineOrder;
import com.mine.pojo.MineOrderShipping;
import com.mine.result.CodeMsg;
import com.mine.result.Result;
import com.mine.service.OrderService;
import com.mine.vo.GoodsQueryVo;
import com.mine.vo.OrderVo;

@Controller
public class OrderController {

	@Autowired
	OrderService orderService;
	
/*****************************************通用订单处理(orderType=1、2)*********************************************************/
/*
 * 订单状态业务逻辑：
 * 
 * 普通订单处理逻辑: 先付费模式： 用户下单(待付款) ->   已付款(等待商家接单) -> 商家已接单(准备商品) ->    商家已发货(送货中) ->   待评价(已送达,已收货，交易完成)   -> 已评价    ->   已取消
 * 				   对应订单状态:orderState=1        orderState=2      orderState=3            orderState=4            orderState=5         orderState=6    orderState=7
 * 					买家操作：(去支付/联系商家/取消订单) (取消订单/联系商家) (取消订单/联系商家/确认收货) (取消订单/联系商家/确认收货) (联系商家/去评价/再次购买) (联系商家/再次购买) (联系商家/再次购买)
 * 					商家操作：  (联系买家)  (联系买家/确认接单/取消订单) (联系买家/确认发货(准备完成)/取消订单) (联系买家/已送达/取消订单)  (联系买家)           (联系买家)        (联系买家)
 * 				货到付款模式： 用户下单(等待商家接单)-> 商家已接单(准备商品) -> 商家已发货(送货中) -> 待付款(已送达/已收货) ->  待评价(交易完成) ->   已评价     ->   已取消
 *                 对应订单状态:orderState=2      orderState=3           orderState=4       orderState=1        orderState=5     orderState=6  orderState=7
 *                 
 * 到店消费订单处理逻辑: 
 *    先付费模式： 用户下单(待付款)->已付款(等待商家接单)->商家确认接单(准备商品) -> 商家准备完成(等待用户确认)-> (用户确认已收货/交易完成)待评价-> 已评价    ->   已取消
 *  对应订单状态:   orderState=1     orderState=2      orderState=3        orderState=4                  orderState=5      orderState=6   orderState=7
 * 				 
 *  货到付款模式： 用户下单(等待商家接单)->商家确认接单(准备商品) ->商家准备完所有商品(等待用户确认支付)-> 待付款(已确认/已收货)-> (用户已支付/交易完成)待评价 -> 已评价    ->   已取消
 *  对应订单状态:   orderState=2        orderState=3             orderState=4                  orderState=1       orderState=5       orderState=6   orderState=7
 *  
 *  orderState： 1.待付款 2.等待商家确认接单 3.商家准备商品中(商家已接单) 4.商家已发货(送货中/商品已准备完/待用户确认) 5.待评价(交易已完成) 6.用户已评价 7.已取消
 */
	
	/************************************************买家订单操作********************************************************/
	//买家创建订单 创建两种订单类型 设置orderType 1.默认(普通订单)  2.到店消费产生订单(如点餐) 
	@RequestMapping(value="/order/create/{token}",method=RequestMethod.POST)
	@ResponseBody
	public Result createOrder(@PathVariable String token, @RequestBody List<OrderVo>  list){
		System.out.println(list);
		if (token==null||list==null||list.size()==0) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		for (int i = 0; i < list.size(); i++) {
			OrderVo orderVo = list.get(i);
			MineOrder order = orderVo.getOrder();
			if (order==null||order.getOrderType()==null) { //orderType 1.默认(普通订单)  2.到店消费产生订单(如点餐) 
				return Result.error(CodeMsg.BIND_ERROR);
			}
			if (order.getPaymentType()==null) { //支付类型
				return Result.error(CodeMsg.BIND_ERROR);
			}
		}
		Result result = orderService.saveOrder(token, list);
		return result;
	}
	
	//买家根据订单id查询订单详情
	@RequestMapping(value="/order/{orderId}/{token}")
	@ResponseBody
	public Result getOrderDetail(@PathVariable String orderId,@PathVariable String token){
		if (orderId==null||token==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = orderService.selectOrderDetail(orderId,token);
		return result;
	}

	//买家取消订单
	@RequestMapping(value="/order/cancel/{token}")
	@ResponseBody
	public Result cancelOrder(@PathVariable String token, @RequestBody MineOrder order){
		if (order==null||token==null||order.getOrderId()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		if (order.getRemark()==null) {
			return Result.error("请填写取消订单原因！");
		}
		
		Result result = orderService.modifyOrderCancel(order,token);
		return result;
	}
	
	//买家根据用户token查询订单信息 type=0查询全部 type=1待付款 type=2待收货 type=3已完成 type=4已取消 ....代表查询不同订单状态 
	@RequestMapping(value="/order/list/{type}/{token}/{size}/{page}")
	@ResponseBody
	public Result getOrderList(@PathVariable Integer type,@PathVariable String token,@PathVariable Integer size,@PathVariable Integer page){
		if (token==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		if (type==null) {
			type = 0;//未设置，设置默认值为0
		}
		if (page<=0) {
			page=1;
		}
		if (size<=0) {
			size = 10;
		}
		
		Result result = orderService.selectOrderList(type,token,size,page);
		return result;
	}
	
	//买家支付订单
	@RequestMapping(value="/order/pay/{orderId}/{token}")
	@ResponseBody
	public Result payOrder(@PathVariable String orderId,@PathVariable String token){
		if (orderId==null||token==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = orderService.modifyOrderPay(orderId,token);
		return result;
	}
	//买家确认收货(完成订单/或货到付款去支付)
	@RequestMapping(value="/order/arrived/{orderId}/{token}")
	@ResponseBody
	public Result arrivedOrder(@PathVariable String orderId,@PathVariable String token){
		if (orderId==null||token==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = orderService.modifyOrderArrived(orderId,token);
		return result;
	}
	
	/************************************************商家订单操作********************************************************/
	//卖家查询订单信息 type=0查询全部 type=1待接单 type=2待发货 type=3已完成 type=4已取消 ....代表查询不同订单状态 
	@RequestMapping(value="/order/seller/list/{type}/{token}/{size}/{page}")
	@ResponseBody
	public Result sellerGetOrderList(@PathVariable Integer type,@PathVariable String token,@PathVariable Integer size,@PathVariable Integer page){
		if (token==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		if (type==null) {
			type = 0;//未设置，设置默认值为0
		}
		if (page<=0) {
			page=1;
		}
		if (size<=0) {
			size = 10;
		}
		
		Result result = orderService.selectSellerOrderList(type,token,size,page);
		return result;
	}
	//商家确认接单
	@RequestMapping(value="/order/seller/taking/{orderId}/{token}")
	@ResponseBody
	public Result sellerTakingOrder(@PathVariable String orderId,@PathVariable String token){
		if (orderId==null||token==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = orderService.modifyOrderSellerTakingOrder(orderId,token);
		return result;
	}
	
	//商家发货 如果有物流信息，设置物流信息
	@RequestMapping(value="/order/seller/consign/{token}", method=RequestMethod.POST)
	@ResponseBody
	public Result sellerOrderConsign(@PathVariable String token,@RequestBody MineOrder order){
		if (order==null||order.getOrderId()==null||token==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = orderService.modifyOrderSellerConsign(order,token);
		return result;
	}
	
	//商家确认送达订单
	@RequestMapping(value="/order/seller/complete/{orderId}/{token}")
	@ResponseBody
	public Result sellerCompleteOrder(@PathVariable String orderId,@PathVariable String token){
		if (orderId==null||token==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = orderService.modifyOrderSellerCompleteOrder(orderId,token);
		return result;
	}
	
	//货到付款 商家确认用户已支付 cash on delivery (COD); COD
	@RequestMapping(value="/order/seller/complete/cod/{orderId}/{token}")
	@ResponseBody
	public Result sellerCompleteCODOrder(@PathVariable String orderId,@PathVariable String token){
		if (orderId==null||token==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		
		Result result = orderService.modifyOrderSellerCompleteCODOrder(orderId,token);
		return result;
	}
	
	//商家取消订单
	@RequestMapping(value="/order/seller/cancel/{token}", method=RequestMethod.POST)
	@ResponseBody
	public Result sellerCancelOrder(@PathVariable String token,@RequestBody MineOrder order){
		System.out.println(order);
		if (order==null||token==null||order.getOrderId()==null) {
			System.out.println("------1");
			return Result.error(CodeMsg.BIND_ERROR);
		}
		if (order.getRemark()==null) {
			return Result.error("请输入取消订单原因");
		}
		Result result = orderService.modifySellerCancelOrder(order,token);
		return result;
	}
	
	
	
}
